package com.tuanzi.loan.business.repository.receipt.user.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanReceiptSystemUser.LOAN_RECEIPT_SYSTEM_USER;

import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.receipt.user.custom.ReceiptSystemUserRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.receipt.user.ReceiptSystemUserSearchRequest;
import com.tuanzi.loan.vo.response.receipt.user.ReceiptSystemUserSearchResponse;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

/**
 * @author Ray 2017/09/21
 */
@Repository
@SuppressWarnings({ "rawtypes" })
public class ReceiptSystemUserRepositoryImpl extends JOOQBaseRepository implements ReceiptSystemUserRepositoryCustom {

	@Override
	protected TableImpl getTable() {
		return LOAN_RECEIPT_SYSTEM_USER;
	}

	@Override
	public Page<ReceiptSystemUserSearchResponse> list(ReceiptSystemUserSearchRequest searchRequest, Pageable pageable) {
		fields(LOAN_RECEIPT_SYSTEM_USER.fields());

		eq(LOAN_RECEIPT_SYSTEM_USER.STATUS, searchRequest.getStatus());
		in(LOAN_RECEIPT_SYSTEM_USER.STATUS, searchRequest.getLimitStatus());
		if (searchRequest.getParent() != null) {
			if (searchRequest.getParent()) {
				isNull(LOAN_RECEIPT_SYSTEM_USER.PARENT);
			} else {
				isNotNull(LOAN_RECEIPT_SYSTEM_USER.PARENT);
			}
		}
		contains(LOAN_RECEIPT_SYSTEM_USER.NAME, searchRequest.getName());
		contains(LOAN_RECEIPT_SYSTEM_USER.ACCOUNT, searchRequest.getAccount());
		contains(LOAN_RECEIPT_SYSTEM_USER.EMAIL, searchRequest.getEmail());
		contains(LOAN_RECEIPT_SYSTEM_USER.PHONE, searchRequest.getPhone());
		contains(LOAN_RECEIPT_SYSTEM_USER.ORGANIZATION, searchRequest.getOrganization());
		contains(LOAN_RECEIPT_SYSTEM_USER.WECHAT, searchRequest.getWeChat());
		return search(pageable, new ReceiptSystemUserSearchResponseRecordMapper());
	}

	private final class ReceiptSystemUserSearchResponseRecordMapper implements RecordMapper<Record, ReceiptSystemUserSearchResponse> {

		@Override
		public ReceiptSystemUserSearchResponse map(Record record) {
			ReceiptSystemUserSearchResponse response = new ReceiptSystemUserSearchResponse();
			response.setId(record.get(LOAN_RECEIPT_SYSTEM_USER.ID));
			response.setName(record.get(LOAN_RECEIPT_SYSTEM_USER.NAME));
			response.setAccount(record.get(LOAN_RECEIPT_SYSTEM_USER.ACCOUNT));
			response.setEmail(record.get(LOAN_RECEIPT_SYSTEM_USER.EMAIL));
			response.setPhone(record.get(LOAN_RECEIPT_SYSTEM_USER.PHONE));
			response.setParent(record.get(LOAN_RECEIPT_SYSTEM_USER.PARENT));
			response.setStatus(record.get(LOAN_RECEIPT_SYSTEM_USER.STATUS).intValue());
			response.setOrganization(record.get(LOAN_RECEIPT_SYSTEM_USER.ORGANIZATION));
			response.setLastLoginTime(record.get(LOAN_RECEIPT_SYSTEM_USER.LAST_LOGIN_TIME));
			response.setWeChat(record.get(LOAN_RECEIPT_SYSTEM_USER.WECHAT));
			return response;
		}
	}
}
